<?php

class MallOrderController extends Controller
{
    /*
    * 获取主订单列表分页
    *
    */
    public function actionGetParentOrderList()
    {

        try {
            //参数验证

            //检测
            $user_id = SessionService::getUserIDBySessionid();


            $searchsql = Util::initSearchSql();

            $searchsql = Util::getSerarchStr($searchsql,"status","status");

            //获取count
            $totalcount = MallOrderParent::model()->count($searchsql);


            //排序
            $searchsql = $searchsql . " order by create_time desc";

            //加上分页控制
            $searchsql = Util::addLimitSql($searchsql);

            //获取list
            $list = MallOrderParent::model()->findAll($searchsql);


            Util::responseList($totalcount, $list);

        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }


    }

    /*
    * 获取子订单分页
    *
    */
    public function actionGetChildOrderList()
    {

        try {
            //参数验证

            //检测
            $user_id = SessionService::getUserIDBySessionid();

            $parent_id = Util::getParamNotEmpty("parent_id");


            $searchsql = Util::initSearchSql();


            $searchsql = Util::getSerarchStr($searchsql,"parent_id","parent_id");


            //获取count
            $totalcount = MallOrderChild::model()->count($searchsql);

                //排序
                $searchsql = $searchsql . " order by create_time desc";

                //加上分页控制
                $searchsql = Util::addLimitSql($searchsql);

                //获取list
                $list = MallOrderChild::model()->findAll($searchsql);

                foreach($list as $key => $value){
                    $searchsql = Util::initSearchSql();
                    $value->detail_info = MallGoodsService::findspecsandattrs($searchsql,$value->goods_id);
                }


                Util::responseList($totalcount, $list);

            } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }


    }


    /*
    * 接单
    *
    */
    public function actionSetreceive()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $obj_id = Util::getParamNotEmpty("obj_id");


            //验证存在
            $obj = MallOrderParent::model()->findByPK($obj_id);

                Util::checkExistAndNotDelete($obj);

                //修改信息

                $obj->status = MsgConst::MALLORDER_STATUE_ALREADY_RECEIVE;

                //保存
                Util::saveObjData($obj);



                //返回数据
                Util::responseMsg(MsgConst::RETURN_SUCCESS);


            } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }


    /*
    * 送货
    *
    */
    public function actionSetsend()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $obj_id = Util::getParamNotEmpty("obj_id");


            //验证存在
            $obj = MallOrderParent::model()->findByPK($obj_id);

            Util::checkExistAndNotDelete($obj);

            //修改信息

            $obj->status = MsgConst::MALLORDER_STATUE_ALREADY_SEND;

            //保存
            Util::saveObjData($obj);



            //返回数据
            Util::responseMsg(MsgConst::RETURN_SUCCESS);


        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }

}